package com.gxa.j259.springcloud.f.hystrix.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author thinknovo
 * @version 1.0
 */
@Configuration
public class FeignRequestHeaderConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {  // apply 添加
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
        if(servletRequestAttributes != null) {
            HttpServletRequest request = servletRequestAttributes.getRequest();
            String token = request.getHeader("token");
            System.out.println("FeignRequestHeaderConfig token=" + token);
            requestTemplate.header("token", token);
        }
    }
}
